package cii_lib;

import java.util.Vector;

import componentEnhancement.ComponentContent;

public class CCII_COMPONENT extends CCII_TABLE_INDEX implements ComponentContent {
	
	 //TODO To jest do generacji !
	  private Vector<CCII_ACCESS_INTERF> interfaces = new Vector<CCII_ACCESS_INTERF>();
	  private Vector<CCII_ACCESS_PARAM> params = new Vector<CCII_ACCESS_PARAM>();
	  private Vector<CCII_COMPONENT> children = new Vector<CCII_COMPONENT>();
	  //TODO
  private CCII_COMPONENT _parent;
  
  //WORKAROUND
  public CCII_COMPONENT(CCII_TABLE_INDEX itab, int index, CCII_COMPONENT parent) {
    super(itab,1);
    if (parent!=null) parent.insertChild(this);
    System.out.println("creating component name: "+ GetItem().Name+" "+GetItem().CompID+" "+GetItem().ParIntVal);
    _parent=parent;
    if (
	GetItem().CompID!=GetCompID()
	|| GetItem().Name==null || GetItem().Type!=CIIlib.TABLE_TYPE.COMP
	|| GetItem().Access!=CIIlib.TABLE_ACCESS.NA || GetItem().Repeat<0 || GetItem().Address!=-1
	|| GetItem().Repeat==0 && (GetItem().Width!=-1 || GetItem().Number!=-1)
	|| GetItem().Repeat>0 && (GetItem().Width<1 || GetItem().Number<1 || GetItem().Position!=index)//FIXME dlaczego position 
	|| GetItem().ParIntVal<-1 || GetItem().ParStrVal==null
      )
      try {
	throw new CiiLibException("CCII_COMPONENT",CiiLibException.MSG.TABLE_ERROR);
      } catch (CiiLibException e) {e.printStackTrace();}
  }
  
  //
  
  public CCII_COMPONENT(CCII_TABLE_INDEX itab, int index, CCII_COMPONENT parent,
		  Vector<CCII_ACCESS_INTERF> interfaces,Vector<CCII_ACCESS_PARAM> params) {
    super(itab,1);
    if (parent!=null) parent.insertChild(this);
    System.out.println("creating component name: "+ GetItem().Name+" "+GetItem().CompID+" "+GetItem().ParIntVal);
    setInterfaces(interfaces);
    setParams(params);
    _parent=parent;
    if (
	GetItem().CompID!=GetCompID()
	|| GetItem().Name==null || GetItem().Type!=CIIlib.TABLE_TYPE.COMP
	|| GetItem().Access!=CIIlib.TABLE_ACCESS.NA || GetItem().Repeat<0 || GetItem().Address!=-1
	|| GetItem().Repeat==0 && (GetItem().Width!=-1 || GetItem().Number!=-1)
	|| GetItem().Repeat>0 && (GetItem().Width<1 || GetItem().Number<1 || GetItem().Position!=index)//FIXME dlaczego position 
	|| GetItem().ParIntVal<-1 || GetItem().ParStrVal==null
      )
      try {
	throw new CiiLibException("CCII_COMPONENT",CiiLibException.MSG.TABLE_ERROR);
      } catch (CiiLibException e) {e.printStackTrace();}
  }
  public boolean Exist() {return((GetItem()==null || GetItem().Repeat==0)?false:true);};
  public CCII_COMPONENT Parent() {return(_parent);};
  public int ParentID() {return(GetItem().ParIntVal);};
  public String Type()  {return(GetItem().ParStrVal);};
  public String Name()  {return(GetItem().Name);};
  public String PathName()  {return(_parent==null?"":CIIlib.PathCompName(Parent())+"."+Name()+
		  "["+Index()+"]");};
  public int DataSize() {return(GetItem().Width);};
  public int AddrSize() {return(GetItem().Number);};
  public int Number()   {return(GetItem().Repeat);};
  public int Index()    {return(GetItem().Position);}
/* (non-Javadoc)
 * @see componentEnhancement.ComponentContent#getInterfaces()
 */
  public Vector<CCII_ACCESS_INTERF> getInterfaces() {
		return interfaces;
	}
	public void setInterfaces(Vector<CCII_ACCESS_INTERF> interfaces) {
		this.interfaces = interfaces;
	}
	public Vector<CCII_ACCESS_PARAM> getParams() {
		return params;
	}
	public void setParams(Vector<CCII_ACCESS_PARAM> params) {
		this.params = params;
	};
	public void insertChild(CCII_COMPONENT child) {
//		System.out.println("jestem dziecko"); //FIXME
		children.add(child);
	}
	public Vector<CCII_COMPONENT> getChildren() {
		return children;
	}
	
};
